package cn.eng.basic.controller;

import cn.eng.basic.dto.LoginDto;
import cn.eng.basic.exception.BusinessException;
import cn.eng.basic.util.AjaxResult;
import cn.eng.user.service.ILogininfoService;
import cn.eng.user.service.IWxuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILogininfoService logininfoService;
    @Autowired
    private IWxuserService wxuserService;

    /**
     * 接口: 账户登录
     * @param loginDto
     * @return
     */
    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginDto loginDto){
        try {
            Map<String,Object> map = logininfoService.accountLogin(loginDto);
            return AjaxResult.me().setObj(map);
        } catch (BusinessException e) {
           return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙,请稍后再试");
        }
    }

    /**
     * 扫码免密登录
     * @param code
     * @return
     */
    @GetMapping("/wechat/{code}")
    public AjaxResult wechatLogin(@PathVariable("code") String code){
        try {
            System.out.println( " 999999"+code);
            return logininfoService.wechatLogin(code);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙,请稍后再试");
        }
    }

    /**
     * 微信绑定接口
     * @param map
     * @return
     */
    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody Map<String,String> map){

        try {
            System.out.println(map);
            return logininfoService.wechatBinder(map);
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙,请稍后再试");
        }
    }
}
